/*
 * =====================================================================================
 *
 *       Filename:  josephus.cpp
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  02/19/2000 06:32:34 AM
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Chen Ken (Ken), zlchen.ken@gmail.com
 *        Company:  EMC
 *
 * =====================================================================================
 */

#include <iostream>
using namespace std;

int LastRemaining_Solution2(int n, unsigned int m)
{
  if(n <= 0 || m < 0)
  {
    return -1;
  }
  int lastinteger = 0;
  for (int i = 2; i <= n; i ++) 
  {
    lastinteger = (lastinteger + m) % i;
  }
  return lastinteger;
}


int main(int argc, char* argv[])
{
  cout << LastRemaining_Solution2(4, 2) << "\n";
}
